﻿using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using log4net;

namespace AgileDashboard.AppCode
{
    /// <summary>
    /// A simple DTO for storing team status data that can be cached 
    /// </summary>
    [DataContract(Name = "teamStatus")]
    public class TeamStatus : VersionOneEntity
    {
        private static Dictionary<string, string> _versionOneAttributeNameDictionary;
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "iterationSchedule")]
        public string IterationSchedule { get; set; }

        [DataMember(Name = "currentIterationCommitmentHours")]
        [VersionOneAttributeName("Workitems[AssetState!='Dead';Timebox.Name='{0}';IsDeleted='False'].DetailEstimate.@Sum")]
        public double CurrentIterationCommitmentHours { get; set; }

        [DataMember(Name = "currentIterationToDoHours")]
        [VersionOneAttributeName("Workitems[AssetState!='Dead';Timebox.Name='{0}';IsDeleted='False'].ToDo.@Sum")]
        public double CurrentIterationToDoHours { get; set; }

        [DataMember(Name = "currentIterationDoneHours")]
        [VersionOneAttributeName("Workitems[AssetState!='Dead';Timebox.Name='{0}';IsDeleted='False'].Actuals.Value.@Sum")]
        public double CurrentIterationDoneHours { get; set; }

        [DataMember(Name = "currentIterationCommitmentHoursMinusToDo")]
        public double CurrentIterationCommitmentHoursMinusToDo
        {
            get
            {
                double done = CurrentIterationCommitmentHours - CurrentIterationToDoHours;
                return done < 0 ? 0 : done; // bounds checking - look for negative values and set them to zero.
            }
        }

        public static Dictionary<string, string> VersionOneAttributeNameDictionary
        {
            get
            {
                if (Log.IsDebugEnabled) Log.DebugFormat("Returning instance of TeamStatus Attribute Name Dictionary");
                return _versionOneAttributeNameDictionary ?? (_versionOneAttributeNameDictionary = BuildVersionOneAttributeDictionary<TeamStatus>());
            }
        }
    }
}
